#include "MyFormBase.h"

MyFormBase::MyFormBase(QWidget *parent)
		: QWidget(parent),
		Config(new QSettings),
		needQuit(false)
{
//	Config = new QSettings;
	ui.setupUi(this);
	this->addAction(ui.actionQuit);
	this->createTrayIcon();
	ui.uploadProcessBar->setVisible(false);
	//
	if ( Config->contains("resTemplateHtml") ) {
		ui.plainTextEdit->setText(Config->value("resTemplateHtml").toString());
	} else {
		ui.plainTextEdit->setPlainText("html: %HTML%\n Image: %IMAGE%\n Thumb: %THUMB%\n");
		saveResTemplate();
	}
	//
	resetUseHtml();
	restoreUploadedTab();
	restoreAutoCopy();
	resetDontClearQueue();
	//
	loadLangs();
}

void MyFormBase::closeEvent(QCloseEvent *event) // instead of destructor...
{
	if ( this->needQuit ) {
		Config->setValue("geometry", this->saveGeometry());
		Config->setValue("splitter", ui.splitter->saveState());
		Config->setValue("splitter_2", ui.splitter_2->saveState());
		Config->setValue("splitter_3", ui.splitter_3->saveState());
		Config->setValue("language", ui.langChooser->currentText());
		event->accept();
	} else {
		event->ignore();
		this->hide();
	}
}

MyFormBase::~MyFormBase() {
	Config->deleteLater();
	trayIcon->deleteLater();
}

/* PROPERTIES */

void MyFormBase::setDontClearQueue(const bool enabled) {
	Config->setValue("dontClearQueue", enabled);
	sinchronizeCheckBox(ui.dontClearQueue, enabled);
	sinchronizeCheckBox(ui.dontClearQueuePref, enabled);
	ui.dontClearQueue->setEnabled(!enabled);
}

void MyFormBase::setUseHtml(const bool enable) {
	Config->setValue("useHtml", enable);
	sinchronizeCheckBox(ui.useHtml, enable);
}

void MyFormBase::autoCopyToggled(const bool enabled) {
	Config->setValue("autoCopy", enabled);
	sinchronizeCheckBox(ui.copyResultOnComplete, enabled);
}

void MyFormBase::enableUploadedTab(const bool enable) {
	Config->setValue("uploadedTab", enable);
	sinchronizeCheckBox(ui.enableLinksTabChBox, enable);
	if ( enable ) {
		ui.tabWidget->insertTab(1, ui.uploadedTab, QString());
		ui.tabWidget->setTabText(1, QApplication::translate("MyForm", "Uploaded Links", 0, QApplication::UnicodeUTF8));
		ui.uploadedTab->layout()->addWidget(ui.scrollAreaForUploaded);
	} else {
		ui.tabWidget->removeTab(ui.tabWidget->indexOf(ui.uploadedTab));
		ui.splitter_2->addWidget(ui.scrollAreaForUploaded);
	}

}

void MyFormBase::loadLangs()
{
	trans.loadFiles();

	ui.langChooser->addItem("English");
	foreach(QString lang, trans.foundLangs()) 	{
		ui.langChooser->addItem(lang);
	}
	connect(ui.langChooser, SIGNAL(activated(QString)), &trans, SLOT(retranslate(QString)));
	connect(&trans, SIGNAL(retranslateUi()), this, SLOT(retranslateUi()));

	this->restoreLang();
}

void MyFormBase::restoreLang()
{
	int index = ui.langChooser->findText(Config->value("language", "English").toString());
	if ( index >= 0 ) // found one
	{
		ui.langChooser->setCurrentIndex(index);
		trans.retranslate(ui.langChooser->currentText());
	} else {
		Config->setValue("language", "English");
	}
}

void MyFormBase::saveResTemplate() const // textEdit in preset tab.
{
	Config->setValue("resTemplateHtml",  ui.plainTextEdit->toHtml());
	Config->setValue("resTemplatePlain", ui.plainTextEdit->toPlainText());
}

void MyFormBase::nextStackedWidget() const// buttons for switching input plugins
{
	int next = ui.stackedWidget->currentIndex();
	int total = ui.stackedWidget->count();
	if ( ++next >= total )
		next = 0;
	ui.stackedWidget->setCurrentIndex(next);
}

void MyFormBase::prevStackedWidget() const// buttons for switching input plugins
{
	int next = ui.stackedWidget->currentIndex();
	if ( --next < 0 )
		next = ui.stackedWidget->count() - 1;
	ui.stackedWidget->setCurrentIndex(next);
}

void MyFormBase::trayAction(const QSystemTrayIcon::ActivationReason reason)
{
	if (reason == QSystemTrayIcon::Trigger) {
		this->setVisible(!this->isVisible());
		if ( this->isVisible() )
			this->showNormal();
	}
}

void MyFormBase::createTrayIcon()
{
	trayIcon = new TrayIcon(this);
	trayIcon->setIcon(this->windowIcon());
	//
	QMenu *trayIconMenu = new QMenu(this);
	trayIconMenu->addAction(ui.actionStart);
	trayIconMenu->addAction(ui.actionCancel);
	trayIconMenu->addSeparator();
	trayIconMenu->addAction(ui.actionQuit);
	trayIcon->setContextMenu(trayIconMenu);
	//
	ui.actionCancel->setEnabled(false);
	trayIcon->show();
	//
	connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayAction(QSystemTrayIcon::ActivationReason)));
//	connect(trayIcon, SIGNAL(setPreview(QString)), this, SLOT(setPreview(QString))); // for future :-)
}



